<?php
/**
 * Displays pages for static content
 *
 * ACCESS: PUBLIC
 *
 * @author Chris Lunsford
 * @version 1.0
 */
class Xa extends Controller {
	
	/**
	 * Class constructor
	 */
	function __construct() {
		parent::__construct();
		
		$this->badger->checkRemember();
	}
	
	/******************** URL Functions ********************/
	
	function index() {
		$data = $this->_buildContent();
		$data['pagetype'] = 'nobg';
		$this->load->model('BlogModel');
		$data['posts'] = $this->BlogModel->getPosts('public','published',3);
		$this->load->view('xa/home', $data);
	}

	/**
	 * Displays about pages: "who we are" "what we're doing" "what we believe"
	 */
	function about() {
		$data = $this->_buildContent();
		if ($this->uri->segment(3) == "beliefs") {
			$this->load->view('xa/beliefs', $data);
		} elseif ($this->uri->segment(3) == "what") {
			$this->load->view('xa/what', $data);
		} else {
			$this->load->view('xa/about', $data);
		}
	}
	
	/**
	 * Displays resource pages: "links" "books" "podcasts" and "international student resources"
	 */
	function resources() {
		$data = $this->_buildContent();
		if ($this->uri->segment(3) == "podcasts") {
			$this->load->view('xa/resources_podcasts', $data);
		} elseif ($this->uri->segment(3) == "books") {
			$this->load->view('xa/resources_books', $data);
		} elseif ($this->uri->segment(3) == "international") {
			$this->load->library('validation');
			$this->load->view('xa/resources_international', $data);
		} else {
			$this->load->view('xa/resources_links', $data);
		}
	}
	
	/**
	 * Displays contact submission form and contact email addresses
	 */
	function contact() {
		$this->load->library('validation');
		
		$rules['name'] = "trim|required";
		$rules['email'] = "trim|required|valid_email";
		$rules['message'] = "trim|required";
		$this->validation->set_rules($rules);
		
		$fields['name']	= 'Name';
		$fields['email']	= 'Email Address';
		$fields['message'] = 'Message';
		$this->validation->set_fields($fields);
	
		$this->validation->set_error_delimiters('<div class="error">','</div>');
		
		if($this->validation->run() == FALSE) {
			$data = $this->_buildContent();
			$this->load->view('xa/contact',$data);
		} else {
			$this->session->set_flashdata('msg_info', 'Thanks for contacting us.  We\'ll get in touch with you soon.');
					
			$name = $this->input->post('name',TRUE);
			$email = $this->input->post('email',TRUE);
			$message = $this->input->post('message',TRUE);
				
			//send Brian an email
			$this->load->library('email');
			$this->email->from('donotreply@xa-ncsu.com','Chi Alpha Christian Fellowship');
			$this->email->to('brianmindyxa@gmail.com');
			$this->email->subject('New Contact Form Submission');
			$this->email->message('Name: ' . $name .'. Email: ' . $email .' Message: '. $message);
			
			$this->email->send();
			redirect('xa/contact');
		}
	}
	
	/**
	 * Displays map and directions to the XA House
	 */
	function house() {
		$data = $this->_buildContent();
		$data['extraBody'] = 'onload="load()" onunload="GUnload()"';
		$this->load->view('xa/house', $data);
	}
	
	/**
	 * Displays a google calendar -> will be replaced with our own calendar function
	 */
	function events() {
		$data = $this->_buildContent();
		$data['pagetype'] = 'nobg';
		$this->load->view('xa/events', $data);
	}
	
	function lifegroups() {
		$this->load->library('validation');
		
		$rules['name'] = "trim|required";
		$rules['email'] = "trim|required|valid_email";
		$rules['message'] = "trim|required";
		$this->validation->set_rules($rules);
		
		$fields['name']	= 'Name';
		$fields['email']	= 'Email Address';
		$fields['message'] = 'Message';
		$this->validation->set_fields($fields);
	
		$this->validation->set_error_delimiters('<div class="error">','</div>');
		
		if($this->validation->run() == FALSE) {
			$data = $this->_buildContent();
			$data['pagetype'] = 'nobg';
			//$tabs = 'look like the others';
			//bottomrightcorner.box = subtle leaf pattern;
			//box[sarah] = doesn't close**';
			$this->load->view('xa/lifegroups', $data);
		} else {
			$name = $this->input->post('name',TRUE);
			$email = $this->input->post('email',TRUE);
			$message = $this->input->post('message',TRUE);
			$recip = $this->input->post('contact_who', TRUE);
			
			$this->load->library('email');
			$this->email->from('donotreply@xa-ncsu.com','Chi Alpha Christian Fellowship');
			if ($recip == "") $leaders = "None";
			else $leaders = $this->UserModel->getLeaders($this->UserModel->getGroupByName($recip)->groupid);
			if ($leaders == "None") {
				$this->email->to('brianmindyxa@gmail.com');
				$this->session->set_flashdata('msg_info', 'Your message has been sent to the campus pastor.');
			} elseif ($leaders->num_rows() > 1) {
				foreach ($leaders->result() as $row) {
					$email_array[] = $row->user->email;
					$name_array[] = $row->user->firstname;
				}
				
				$this->email->to(implode(',', $email_array));
				$this->session->set_flashdata('msg_info', 'Your message has been sent to '.implode(' and ', $name_array).'.');
			} else {
				$this->email->to($leaders->row()->user->email);
				$this->session->set_flashdata('msg_info', 'Your message has been sent to '. $leaders->row()->user->firstname.'.');
			}
					
			$this->email->subject('Lifegroup Contact Form Submission');
			$this->email->message('Name: ' . $name .'\n\nEmail: ' . $email .'\n\nMessage: '. $message);
			$this->email->send();
			
			redirect('xa/lifegroups');
		}
		
	}
	
	/******************* Task Functions *******************/

	/**
	 * Displays and processes form for requesting a bible in your language
	 * Found on international resources page.
	 */
	function biyl() {
		$this->load->library('validation');
		
		$rules['name'] = "trim|required";
		$rules['language'] = "trim|required";
		$rules['email'] = "trim|required|valid_email";
		$rules['phone'] = "trim";
		$this->validation->set_rules($rules);
		
		$fields['name']	= 'Name';
		$fields['language']	= 'language';
		$fields['email']	= 'Email Address';
		$fields['phone'] = 'Phone number';
		$this->validation->set_fields($fields);
	
		$this->validation->set_error_delimiters('<div class="error">','</div>');
		
		if($this->validation->run() == FALSE) {
			$data = $this->_buildContent();
			$this->load->view('xa/resources_international',$data);
		} else {
			$this->session->set_flashdata('msg_info', "We\'ve received your submission.  We\'ll be in touch."); 
			$post['name'] = $this->input->post('name',TRUE);
			$post['language'] = $this->input->post('language',TRUE);
			$post['email'] = $this->input->post('email',TRUE);
			$post['phone'] = $this->input->post('phone',TRUE);
			$this->load->model('ContentModel');
			$this->ContentModel->createBIYL($post);
				
			//send Brian an email
			$this->load->library('email');
			$this->email->from('donotreply@xa-ncsu.com','Chi Alpha Christian Fellowship');
			$this->email->to('brianmindyxa@gmail.com');
			$this->email->subject('New Bible in Your Language Submission');
			$this->email->message('Name: ' . $post['name'] .'. Language: ' . $post['language'] .'. Email: ' . $post['email'] .'. Phone: ' . $post['phone'] .'.');
			
			$this->email->send();

			redirect('xa/resources/international');
		}

	}

	/**
	 * Sends an email to admin from contact form submission
	 */
	function contactEmail() {
		$this->load->library('validation');
		
		$rules['name'] = "trim|required";
		$rules['email'] = "trim|required|valid_email";
		$rules['message'] = "trim|required";
		$this->validation->set_rules($rules);
		
		$fields['name']	= 'Name';
		$fields['email']	= 'Email Address';
		$fields['message'] = 'Message';
		$this->validation->set_fields($fields);
	
		$this->validation->set_error_delimiters('<div class="error">','</div>');
		
		if($this->validation->run() == FALSE) {
			$data = $this->_buildContent();
			$this->load->view('xa/contact',$data);
		} else {
			$this->session->set_flashdata('msg_info', 'Thanks for contacting us.  We\'ll get in touch with you soon.');
					
			$name = $this->input->post('name',TRUE);
			$email = $this->input->post('email',TRUE);
			$message = $this->input->post('message',TRUE);
				
			//send Brian an email
			$this->load->library('email');
			$this->email->from('donotreply@xa-ncsu.com','Chi Alpha Christian Fellowship');
			$this->email->to('brianmindyxa@gmail.com');
			$this->email->subject('New Contact Form Submission');
			$this->email->message('Name: ' . $name .'. Email: ' . $email .' Message: '. $message);
			
			$this->email->send();
			redirect('xa/contact');
		}
	}
	
	/**
	 * Sends an email to admin from contact form submission
	 */
	function contactLifegroup() {
		$this->load->library('validation');
		
		$rules['name'] = "trim|required";
		$rules['email'] = "trim|required|valid_email";
		$rules['message'] = "trim|required";
		$this->validation->set_rules($rules);
		
		$fields['name']	= 'Name';
		$fields['email']	= 'Email Address';
		$fields['message'] = 'Message';
		$this->validation->set_fields($fields);
	
		$this->validation->set_error_delimiters('<div class="error">','</div>');
		
		if($this->validation->run() == FALSE) {
			$data = $this->_buildContent();
			$data['pagetype'] = 'nobg';
			$this->load->view('xa/lifegroups', $data);
		} else {
			$this->session->set_flashdata('msg_info', 'Thanks for contacting us.  We\'ll get in touch with you soon.');
					
			$name = $this->input->post('name',TRUE);
			$email = $this->input->post('email',TRUE);
			$message = $this->input->post('message',TRUE);
				
			//send Brian an email
			$this->load->library('email');
			$this->email->from('donotreply@xa-ncsu.com','Chi Alpha Christian Fellowship');
			$this->email->to('brianmindyxa@gmail.com');
			$this->email->subject('New Contact Form Submission');
			$this->email->message('Name: ' . $name .'. Email: ' . $email .' Message: '. $message);
			
			$this->email->send();
			redirect('xa/contact');
		}
	}
	
	/*
	 * Supplies user-specific information to the interface for customization
	 * REQUIRED DATA ARRAY PARAMS:
	 * pagetype = page, page-admin, nobg
	 * loggedin = isset
	 */
	function _buildContent() {
		$data = array();
		if ($this->session->flashdata('msg_info')) $data['msg_info'] = $this->session->flashdata('msg_info');
		if ($this->session->flashdata('msg_error'))	$data['msg_error'] = $this->session->flashdata('msg_error');
		
		if ($this->session->userdata('loggedin')) {
			$this->load->model('UserModel');
			$user = $this->UserModel->getUserByName($this->session->userdata('username'));
			
			$data['con_user'] = $user;
			$data['loggedin'] = TRUE;
		}
		
		return $data;
	}
}
?>